package com.bookheaven.beans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Inject;

import com.bookheaven.BaseLazyDataModel;
import com.bookheaven.core.conroller.base.BaseEntityController;
import com.bookheaven.core.entity.base.BaseEntity;
import com.bookheaven.messages.MessageManager;

public abstract class BookHeavenBean<T extends BaseEntity> implements Serializable {
	
	private static final long	serialVersionUID	= 2797730338194746822L;
	
	@EJB
	BaseEntityController		baseEntityController;
	
	@Inject
	MessageManager				messageManager;
	
	private BaseLazyDataModel<T>	dataModel;
	private T					entity;
	private boolean				editMode;
	private String				searchKey;
	private int					pageSize			= 2;
	
	public abstract T createEntity();
	
	@PostConstruct
	public void postConstruct() {
		dataModel = new BaseLazyDataModel<T>(this);
		clear();
	}
	
	public void clear() {
		setEditMode(false);
		setEntity(createEntity());
	}
	
	public void saveEntity() {
		baseEntityController.save(entity);
		clear();
	}
	
	public void deleteEntity() {
		baseEntityController.delete(entity);
		clear();
	}
	
	public T getEntity() {
		return entity;
	}
	
	public void setEntity(T entity) {
		this.entity = entity;
	}
	
	public boolean isEditMode() {
		return editMode;
	}
	
	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}
	
	public String getSearchKey() {
		return searchKey;
	}
	
	public void setSearchKey(String searchKey) {
		this.searchKey = searchKey;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public BaseEntityController getEntityController() {
		return baseEntityController;
	}
	
	public BaseLazyDataModel<T> getDataModel() {
		return dataModel;
	}
}
